package com.zhanbo.zhanbowymusic.music;


import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import androidx.annotation.Nullable;


import com.mwx.baselib.constant.Constant;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;

public class MusicService extends Service implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener {

    private final IBinder binder = new AudioBinder();
    String  TAG ="MusicService";
    private MediaPlayer mediaPlayer;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return binder;
    }


    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 播放
     * @param url - 音乐播放路径
     */
    public void changeMusic(String url){
        Log.e(TAG, "playMusic: 开始播放 ：" + url);
        //判断播放类是否为空
        if(mediaPlayer == null){
            //如果为空，创建
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnPreparedListener(this);
            //绑定播放完毕监听
            mediaPlayer.setOnCompletionListener(this);
        }

        try {
            // 切歌之前先重置，释放掉之前的资源
            mediaPlayer.reset();
            // 设置播放源
            mediaPlayer.setDataSource(url);
            // 开始播放前的准备工作，加载多媒体资源，获取相关信息
        } catch (IOException e) {
            e.printStackTrace();
            String message = e.getMessage();
            Log.e(TAG, "changeMusic: msg = "+message);
        }
        mediaPlayer.prepareAsync();
        Log.e(TAG, "playMusic: 开始准备 ：" + "prepareAsync");
    }

    //播放结束监听
    @Override
    public void onCompletion(MediaPlayer mp) {
        //当前歌曲播放完毕，通知 Controller 类 继续播放下一首，Event.id = 0
        Log.e(TAG, "onCompletion: 这是播放完毕");
        EventBus.getDefault().post(new OnEvent(Constant.MUSIC_OVER));
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mediaPlayer.start();
    }

    /**
     * 进度条跨进
     * @param progress - 跳掉指定进度
     */
    public void seekTo(int progress){
        mediaPlayer.seekTo(progress);
    }

    /**
     * 暂停
     */
    public void pause(){
        if(mediaPlayer != null && mediaPlayer.isPlaying()) {
            Log.e(TAG, "pause: 暂停" );
            mediaPlayer.pause();
        }
    }

    /**
     * 继续
     */
    public void play(){
        if(mediaPlayer != null ) {
            mediaPlayer.start();
        }
    }

    /**
     * 返回当前播放时间
     * @return
     */
    public int getCurrentDuration(){
        if(mediaPlayer == null){
            return 0;
        }else {
            return mediaPlayer.getCurrentPosition();
        }
    }
    /**
     * 返回歌曲时长
     * @return
     */
    public int getDuration(){
        if(mediaPlayer == null){
            return 0;
        }else {
            return mediaPlayer.getDuration();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: MusicService");
        if(mediaPlayer != null){
            mediaPlayer.stop();
            mediaPlayer.release();
        }

    }

    //为了和Activity交互，我们需要定义一个Binder对象
    public class AudioBinder extends Binder {

        //返回Service对象
        public MusicService getService(){
            return MusicService.this;
        }
    }

}
